﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace RainyPWS.Controls
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:RTextBox runat=\"server\" Caption=\"Caption\"></{0}:RTextBox>")]
    public class RTextBox : TextBox
    {

        public RTextBox()
        {
        }

        private const int PaddingBorderWidth = 8;
        private const int LableMarginWidth = 5;
        private const int DefaultTextBoxWidth = 250;
        private const int DefaultLabelWidth = 80;
        
        [Bindable(true)]
        public string Caption
        {
            get
            {
                String s = (String)ViewState["Caption"];
                return ((s == null) ? "Caption" : s);
            }
            set
            {
                ViewState["Caption"] = value;
            }
        }

        /// <summary>
        /// label宽度
        /// </summary>
        public Unit CaptionWidth
        {
            get
            {
                if (ViewState["CaptionWidth"] != null)
                {
                    return (Unit)ViewState["CaptionWidth"];
                }
                else
                {
                    return new Unit(DefaultLabelWidth);
                }
            }
            set
            {
                if (value.Value > TotalWidth.Value - Width.Value)
                {
                    value = new Unit(TotalWidth.Value - Width.Value);
                }
                ViewState["CaptionWidth"] = value;
                
            }
        }

        public Unit TotalWidth
        {
            get
            {
                if (ViewState["TotalWidth"] != null)
                {
                    return (Unit)ViewState["TotalWidth"];
                }
                else
                {
                    return new Unit(DefaultLabelWidth + DefaultTextBoxWidth);
                }
            }
            set
            {
                if (value.Value < Width.Value + CaptionWidth.Value)
                {
                    value = new Unit(Width.Value + CaptionWidth.Value);
                }
                ViewState["TotalWidth"] = value;
            }
        }

        public override Unit Width
        {
            get
            {
                Unit BorderPaddingWidth;
                if (ViewState["BorderPaddingWidth"] != null)
                {
                    BorderPaddingWidth = (Unit)ViewState["BorderPaddingWidth"];
                }
                else
                {
                    Unit bpw = new Unit(DefaultTextBoxWidth);
                    ViewState["BorderPaddingWidth"] = bpw;
                    base.Width = new Unit(DefaultTextBoxWidth - PaddingBorderWidth);
                    BorderPaddingWidth = new Unit(DefaultTextBoxWidth);
                }
                return BorderPaddingWidth;
            }
            set
            {
                if (value.Value > TotalWidth.Value - CaptionWidth.Value)
                {
                    value = new Unit(TotalWidth.Value - CaptionWidth.Value);
                }
                ViewState["BorderPaddingWidth"] = value;
                base.Width = new Unit(value.Value - PaddingBorderWidth);
            }
        }

        protected override void Render(HtmlTextWriter writer)
        {
            if (CssClass == string.Empty)
            {
                CssClass = "d-form-field d-form-text";
            }
            writer.Write("<div class=\"d-form-item\" style=\"width:" + TotalWidth.Value + "px;\">");
            writer.Write("<label for=\"" + this.ClientID + "\" class=\"d-form-item-label\" style=\"width:" + (CaptionWidth.Value - 5) + "px; margin-right:5px;\">" + Caption + "</label>");
            writer.Write("<div class=\"d-form-item-body\" style=\"width:" + Width.Value + "px;\">");
            base.Render(writer);
            writer.Write("</div>");
            writer.Write("<div class=\"d-clear\"></div>");
            writer.Write("</div>");
        }
    }
}
